import {
  Controller,
  Get,
  Post,
  Body,
  UsePipes,
  ValidationPipe,
  Query,
  UseGuards,
  Req,
} from '@nestjs/common';
import { GameService } from './game.service';
import { CreateGameDto } from './dto/create-game.dto';
import { ApiBearerAuth } from '@nestjs/swagger';
import { Auth } from 'src/common/decorators/auth.decorator';
import { RequirePermission } from 'src/common/decorators/permission.decorator';

// import { UpdateGameDto } from './dto/update-game.dto';

@Auth()
@ApiBearerAuth() //
@Controller('game')
export class GameController {
  constructor(private readonly gameService: GameService) {}

  @RequirePermission(['1007001', '1007002', '1007003'], 'OR')
  @Post('save')
  @UsePipes(new ValidationPipe({ transform: true }))
  create(@Req() request, @Body() createGameDto: CreateGameDto) {
    const user = request.user;
    if (createGameDto.id) {
      createGameDto.updateBy = user.id;
    } else {
      createGameDto.createBy = user.id;
      createGameDto.updateBy = user.id;
    }
    if (!createGameDto.lastProgress) {
      createGameDto.lastProgress =
        createGameDto.progress[createGameDto.progress.length - 1];
    }
    return this.gameService.create(createGameDto);
  }

  @Get('getById')
  getById(@Query('id') id: number) {
    return this.gameService.findOne(id);
  }

  @Get('delete')
  delete(@Query('id') id: number) {
    return this.gameService.removeIds([id]);
  }
}
